package com.saunter.controller;

import com.saunter.model.Department;
import com.saunter.service.DepartmentService;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "管理员编辑部门")
@RestController
@RequestMapping("/admin")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("添加部门")
    @PostMapping("/addDepartment")
    public Map<String, Object> create(@RequestParam("name")String name, @ApiParam(hidden = true) @RequestParam(value = "status", required = false)Integer status) {
        if(1 == departmentService.departmentAdd(new Department(null, name, status))) {
            return Response.success("添加成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("获取部门信息(包含职位)")
    @GetMapping("/getDepartment")
    public Map<String, Object> getDepartmentJob() {
        return Response.success(departmentService.getAllDepartmentJobs());
    }

    @ApiOperation("修改指定部门名称")
    @PostMapping("/editDepartment")
    public Map<String, Object> editName(@RequestParam("id")int department_id, @RequestParam("name")String name) {
        if(1 == departmentService.editName(new Department(department_id, name, null))){
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("删除指定部门")
    @GetMapping("/deleteDepartment/{id}")
    public Map<String, Object> delete(@PathVariable("id")int department_id) {
        if(1 == departmentService.delete(department_id)){
            return Response.success("删除成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }
}
